<!DOCTYPE html>
<html xmlns:sa="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>流程定义列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
    <style>
        /* 自定义模态框样式 */
        #processDiagramModal {
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
            text-align: center;
            height: 100%; /* 确保模态框高度占满整个视图 */
        }

        #processDiagramImage {
            max-width: 100%; /* 确保图片宽度不超过模态框 */
            height: auto; /* 保持图片比例 */
            display: block;
            margin: 0 auto; /* 上下居中 */
            border: 2px solid #ccc; /* 添加外边框，宽度为2px，样式为实线，颜色为浅灰色 */
            padding: 5px; /* 添加内边距，使边框和图片之间有一些空间 */
            background-color: #fff; /* 背景颜色为白色 */
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* 添加轻微的阴影效果 */
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">流程名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="flowName" placeholder="请输入流程名称" class="layui-input"
                               id="flowName">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md " lay-submit lay-filter="form-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="processDefinitionTable" lay-filter="processDefinitionTable"></table>
    </div>
</div>

<script type="text/html" id="form-toolbar">
    <button class="layui-btn  layui-btn-md" sa:hasPermission="processDeploy_add" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        部署
    </button>
</script>
<script type="text/html" id="form-enable">
    <input type="checkbox" name="isSuspended" value="{{d.id}}" lay-skin="switch" lay-text="启用|挂起"
           lay-filter="form-enable" {{ d.isSuspended ? '' : 'checked' }} />
</script>
<script type="text/html" id="processDefinitionBar">
    <button class="layui-btn layui-btn-sm" sa:hasPermission="set_form" lay-event="setForm">
        设置表单
    </button>
    <!--    <button class="layui-btn layui-btn-sm" lay-event="edit"><i-->
    <!--            class="layui-icon layui-icon-edit">编辑</i>-->
    <!--    </button>-->
    <button class="layui-btn layui-btn-danger layui-btn-sm" sa:hasPermission="processDeploy_delete" lay-event="remove">
        删除
    </button>
</script>
<!-- 模态框 -->
<div id="processDiagramModal" style="display: none;">
    <img id="processDiagramImage" src="" alt="流程图" style="max-width: 100%; height: auto;">
</div>

<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">
    let baseURL = /*[[@{/}]]*/ '';
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;
        let form = layui.form;
        let cols = [
            [
                {type: 'checkbox'},
                {field: 'id', title: 'ID', sort: true},
                {
                    field: 'name',
                    title: '流程名称',
                    templet: function (d) {
                        return '<a href="javascript:void(0)" class="process-name" data-id="' + d.id + '" style="color: #00feff">' + d.name + '</a>';
                    }
                },
                {field: 'key', title: '流程键'},
                {field: 'version', title: '版本'},
                {field: 'deploymentId', title: '部署ID'},
                // {field: 'resourceName', title: '资源名称'},
                // {field: 'diagramResourceName', title: '流程图资源名称'},
                {field: 'isSuspended', title: '挂起状态', templet: '#form-enable'},
                {fixed: 'right', title: '操作', toolbar: '#processDefinitionBar'}
            ]
        ]
        // 渲染表格
        table.render({
            elem: '#processDefinitionTable',
            url: baseURL + 'workflow/processDeploy/processDeployList',
            type: 'get',
            toolbar: '#form-toolbar',
            page: true,
            limit: 10,
            limits: [10, 20, 30],
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            cols: cols,
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list,
                };
            },
            response: {
                statusCode: 1000
            }
        });
        table.on('tool(processDefinitionTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'setForm') {
                window.setForm(obj);
            }
        });

        table.on('toolbar(processDefinitionTable)', function (obj) {
            if (obj.event === 'add') {
                window.add(obj);
            }
        });
        // 操作栏
        window.add = function (obj) {
            layer.open({
                type: 2,
                title: '新增流程',
                shade: 0.1,
                area: ['100%', '100%'],
                content: 'process_definition_add'
            });
        }


        form.on('switch(form-enable)', function (obj) {
            let loading = layer.msg('修改中...', {
                icon: 16,
                shade: 0.01,
                time: 0
            });
            $.ajax({
                url: baseURL + 'workflow/processDeploy/enable/' + obj.value,
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 1000) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                    }
                },
                error: function () {
                    layer.close(loading);
                    layer.msg('服务器错误，请稍后再试！', {
                        icon: 2,
                        time: 1000
                    });
                }
            });
        });

        window.setForm = function (obj) {
            window.processDefinitionId = obj.data.id
            $.ajax({
                url: baseURL + 'workflow/processFrom/getFromField',
                type: 'get',
                data: {
                    processDefinitionId: obj.data.id
                },
                success: function (response) {
                    window.form = response.data
                    layer.open({
                        type: 2,
                        title: '挂载表单',
                        shade: 0.1,
                        area: ['100%', '100%'],
                        content: 'process_form_list'
                    });
                },
                error: function (xhr, status, error) {
                    layer.msg('服务器错误：' + error);
                }
            })
        }

        window.remove = function (obj) {
            let data = obj.data;
            // 删除流程
            layer.confirm('确定要删除该流程定义吗？', function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: baseURL + 'workflow/flowable/deleteProcessDefinition',
                    type: 'POST',
                    data: {
                        deploymentId: data.deploymentId,
                        cascade: true,
                    },
                    success: function (response) {
                        layer.close(loading);
                        if (response.code === 1000) {
                            layer.msg(response.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('processDefinitionTable');
                            });
                        } else {
                            layer.msg(response.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }

                    },
                    error: function (xhr, status, error) {
                        layer.msg('删除失败：' + error);
                    }
                });
            });

        }


        //重置按钮
        document.getElementById('resetBtn').addEventListener('click', function () {
            table.reload('processDefinitionTable', {
                page: {curr: 1}
            });
        });


        // 监听流程名称的点击事件
        $(document).on('click', '.process-name', function () {
            let processDefinitionId = $(this).data('id');
            showProcessDiagram(processDefinitionId);
        });

        // 显示流程图的函数
        function showProcessDiagram(processDefinitionId) {
            // 使用 $.ajax 调用后端接口获取流程图
            $.ajax({
                url: baseURL + "workflow/flowable/getProcessDiagram", // 后端接口地址
                type: "POST", // 请求方式为 POST
                data: {processDefinitionId: processDefinitionId}, // 传递参数
                success: function (response) {
                    // 检查返回的状态码
                    if (response.code === 1000) {
                        // 提取 Base64 数据
                        let base64Data = response.data;
                        // 将 Base64 数据转换为图片的 src 属性
                        let imageUrl = "data:image/png;base64," + base64Data;
                        $('#processDiagramImage').attr('src', imageUrl);

                        // 显示模态框
                        layer.open({
                            type: 1,
                            area: ['800px', '600px'],
                            title: '流程图',
                            content: $('#processDiagramModal'),
                            success: function (layero, index) {
                                // 在模态框关闭时释放 URL 对象
                                $(layero).on('hidden.bs.modal', function () {
                                    // 释放 Base64 数据（可选）
                                });
                            }
                        });
                    } else {
                        // 如果返回的状态码不是 200，提示错误信息
                        layer.msg(response.message || '无法加载流程图');
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    // 错误处理
                    console.error('无法加载流程图：', textStatus, errorThrown);
                    layer.msg('无法加载流程图');
                }
            });
        }
    });

</script>


</body>
</html>